﻿using System;
using MaLu.Data;
using Malu.WebTemplates;
using Wicresoft.Framework.Organization;
using System.Configuration;

namespace MaLu.Common
{
    public class GBusiPage : BaseAuthenticationPage
    {
        public int Quarter
        {
            get
            {
                int _q = 0;
                int.TryParse(Request["q"] ?? "0", out _q);
                if (_q == 0)
                    _q = (int)Math.Ceiling(DateTime.Now.Month / 3f);
                return _q;
                
            
            }
        
        }

        public int Year
        {
            get
            {
                int _year = 0;
                int.TryParse(Request["year"] ?? "0", out _year);
                if (_year == 0)
                    _year = DateTime.Today.Year;
                return _year;
            }

        }
        private IDbProvider provider = null;
        protected IDbProvider Provider
        {
            get
            {
                if (provider == null)
                {
                    provider = ProviderManager.Factory.CreateDbProvider();
                    provider.Open();
                   
                }
                return provider;
            }

        }
        public Guid DepartmentID
        {
            get
            {
                if (Request["DepartmentID"] != null)
                    return new Guid(Request["DepartmentID"]);
                if (ViewState["DepartmentID"] == null)
                    ViewState["DepartmentID"] = Guid.Empty;
                return (Guid)ViewState["DepartmentID"];
            }
            set
            {
                ViewState["DepartmentID"] = value;
            }
        }

        private int ibusiadmin = -1;
        private bool isbusiadmin = false;
        public bool IsBusiAdmin
        {
            get
            {
                if (ibusiadmin == -1)
                {
                    isbusiadmin = OrganizationHelper.IsUserInRole(CurrentUser.ID, new Guid(ConfigurationManager.AppSettings["GBusiAdmin"]));
                    ibusiadmin = 0;
                }
                return isbusiadmin;
            }
        
        }

        private int ibusisubadmin = -1;
        private bool isbusisubadmin = false;
        public bool IsBusiSubAdmin
        {
            get
            {
                if (ibusisubadmin == -1)
                {
                    isbusisubadmin = OrganizationHelper.IsUserInRole(CurrentUser.ID, new Guid(ConfigurationManager.AppSettings["GBusiSubAdmin"]));
                    ibusisubadmin = 0;
                }
                return isbusisubadmin;
            }

        }
    }
}
